<template>
  <div class="app-container">
    <split-pane :min-percent='30' :default-percent='30' split="vertical">
      <template slot="paneL">
        <el-card class="m-l-5 dict-tree">
          <mall-category-tree ref="category_tree" @clickNode="handleClickNode" />
        </el-card>
      </template>

      <template slot="paneR">
        <!-- 搜索区域 -->
        <el-card style="margin:0 0 5px 5px" v-show="showSearch">
          <el-form ref="queryForm" :inline="true" :model="queryData">
            <el-form-item label="分类">
              <el-input v-model="catalogName" readonly />
            </el-form-item>
            <el-form-item label="组名" prop="Q_attrGroupName_S_LK">
              <el-input v-model="queryData.Q_attrGroupName_S_LK" placeholder="组名" />
            </el-form-item>
            <el-form-item class="fr p-r-10">
              <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
                搜索
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <!-- table区域 -->
        <el-card class="m-l-5">
          <el-row :gutter="10" class="m-b-5">
            <el-col :span="1.5">
              <el-button v-permission="['mall:product:attr']" size="mini" plain type="primary" icon="el-icon-plus" @click="handleInfo()">
                新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button v-permission="['mall:product:attr']" v-if="selectionData.length > 0" size="mini" plain type="danger" icon="el-icon-delete" @click="handleDelete()">
                删除
              </el-button>
            </el-col>
            <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
          </el-row>
          <el-table
            ref="attr_table"
            v-loading="tableLoading"
            :data="tableData"
            element-loading-text="加载中"
            border
            fit
            highlight-current-row
            show-overflow-tooltip
            height="100px"
            v-el-height-adaptive-table="{bottomOffset: 110}"
            @sort-change="sortChange"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="40" />
            <el-table-column align="center" label="组名" show-overflow-tooltip>
              <template slot-scope="scope">
                <span style="cursor: pointer" @click="handleInfo(scope.row.attrGroupId)">{{ scope.row.attrGroupName }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="图标" width="75">
              <template slot-scope="scope">
                <i :class="'icon ' + scope.row.icon" />
              </template>
            </el-table-column>
            <el-table-column align="center" label="所属分类" prop="catalogName" width="110">
              <template slot-scope="scope">{{ catalogName }}</template>
            </el-table-column>
            <el-table-column align="center" label="描述" prop="describe" width="150" show-overflow-tooltip />
            <el-table-column align="center" label="排序" prop="groupSort" sortable width="75" />
            <el-table-column align="center" label="操作" width="200" fixed="right">
              <template slot-scope="scope">
                <el-link v-permission="['mall:product:attr']" type="primary" icon="el-icon-view" @click="handleInfo(scope.row.attrGroupId)">编辑</el-link>
                <el-link v-permission="['mall:product:attr']" class="m-l-10" type="success" icon="el-icon-news" @click="handleRelation(scope.row.attrGroupId)">关联</el-link>
                <el-link v-permission="['mall:product:attr']" class="m-l-10" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.attrGroupId)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </template>>
    </split-pane>

    <attr-group-form-dialog v-if="openFormDialog" ref="attrGroupForm" @confirmForm="handleFilter" />
    <attr-group-relation-dialog v-if="openRelationDialog" ref="relationDialog" @confirmDialog="handleFilter" />
  </div>
</template>

<script>
import { groupTable, groupDelete } from '@/api/mall/product/attrgroup'
import SplitPane from 'vue-splitpane'
import MallCategoryTree from '@/views/mall/product/category/tree'
import AttrGroupFormDialog from './form/dialog'
import AttrGroupRelationDialog from './relation/dialog'

export default {
  components: {
    SplitPane,
    MallCategoryTree,
    AttrGroupFormDialog,
    AttrGroupRelationDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_catalogId_L_EQ: undefined,
        Q_attrGroupName_S_LK: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: 'groupSort',
        order: 'asc'
      },
      selectionData: [],
      catalogId: undefined,
      catalogName: undefined,
      openFormDialog: false,
      openRelationDialog: false
    }
  },
  methods: {
    handleClickNode(cId, name, level) {
      if (level === 4) {
        this.queryData.Q_catalogId_L_EQ = cId
        this.catalogId = cId
        this.catalogName = name
        this.handleFilter()
      }
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      groupTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.attr_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(id) {
      if (!this.catalogId) {
        this.$modal.msgWarning('请先选择分类')
        return
      }
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.attrGroupForm.init(id, this.catalogId)
      })
    },
    handleRelation(id) {
      this.openRelationDialog = true
      this.$nextTick(() => {
        this.$refs.relationDialog.init(id)
      })
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function(data, index) {
        return data.attrGroupId
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选属性?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        groupDelete({ ids: id }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}

.filter-tree {
  overflow-y: auto;
}

.app-container {
  .filter-tree {
    height: calc(100vh - 190px);
  }
}

.fixed-header+.app-main {
  .app-container {
    .filter-tree {
      height: calc(100vh - 190px);
    }
  }
}

.hasTagsView {
  .app-main {
    .app-container {
      .filter-tree {
        height: calc(100vh - 225px);
      }
    }
  }
}

.hasTagsView {
  .fixed-header+.app-main {
    .app-container {
      .filter-tree {
        height: calc(100vh - 225px);
      }
    }
  }
}
</style>
